home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / util / ppmdiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-22  |  2.2 KB  |  95 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(int argc, char **argv)
  5. {
  6.   FILE *inFile1 = 0;
  7.   FILE *inFile2 = 0;
  8.   FILE *outFile = 0;
  9.   char *outName = 0;
  10.   char ppmString[256];
  11.   int t1, t2, x1, x2, y1, y2, i, max1, max2;
  12.   long long int mse = 0;
  13.   double psnr = 0, rmse = 0;
  14.  
  15.   if ((argc > 5) || (argc == 4)) {
  16.     printf("too much arguments\n");
  17.     return 5;
  18.   }
  19.  
  20.   for (i = 1; i < argc; i++) {
  21.     if (!strcmp(argv[i], "-o")) {
  22.       outName = argv[++i];
  23.       if (!(outFile = fopen(outName, "w"))) {
  24.     printf("failed to open outputfile %s\n", outName);
  25.     return 10;
  26.       }
  27.     }
  28.     else if (!inFile1) {
  29.       if (!(inFile1 = fopen(argv[i], "r"))) {
  30.     printf("failed to open first inputfile %s\n", argv[i]);
  31.     return 10;
  32.       }
  33.     }
  34.     else if (!inFile2) {
  35.       if (!(inFile2 = fopen(argv[i], "r"))) {
  36.     printf("failed to open second inputfile %s\n", argv[i]);
  37.     return 10;
  38.       }
  39.     }
  40.   }
  41.  
  42.   if (!inFile1 || !inFile2) {
  43.     printf("you must specify two sources\n");
  44.     return 5;
  45.   }
  46.   if (!outFile)
  47.     outFile = stdout;
  48.  
  49.   fscanf(inFile1, "P%1d\n%d %d\n%d\n", &t1, &x1, &y1, &max1);
  50.   fscanf(inFile2, "P%1d\n%d %d\n%d\n", &t2, &x2, &y2, &max2);
  51.  
  52.   if ((t1 != t2) || (x1 != x2) || (y1 != y2) || (max1 != max2)) {
  53.     printf("cannot compare pictures with different sizes or formats\n");
  54.     return 5;
  55.   }
  56.   if (!((t1 == 5) || (t1 == 6))) {
  57.     printf("cannot handle this format P%1d\n", t1);
  58.     return 5;
  59.   }
  60.  
  61.   fprintf(outFile, "P5\n%d %d\n255\n", x1, y1);
  62.  
  63.   for (i = 0; i < (x1 * y1); i++) {
  64.     int r, g, b, p;
  65.  
  66.     r = fgetc(inFile2) - fgetc(inFile1);
  67.     mse += (long long int)(r * r);
  68.     r >>= 1;
  69.     r += 127;
  70.     if (t1 == 6) {
  71.       g = fgetc(inFile2) - fgetc(inFile1);
  72.       mse += (long long int)(g * g);
  73.       g >>= 1;
  74.       g += 127;
  75.       b = fgetc(inFile2) - fgetc(inFile1);
  76.       mse += (long long int)(b * b);
  77.       b >>= 1;
  78.       b += 127;
  79.  
  80.       p = (r + g + b) / 3;
  81.       fputc(p, outFile);
  82.     }
  83.     else
  84.       fputc(r, outFile);
  85.   }
  86.  
  87.   psnr = 10 * log10(65536.0 / ((double)mse / (x1 * y1 * (t1 == 6 ? 3 : 1))));
  88.   rmse = sqrt((double)mse / (x1 * y1 * (t1 == 6 ? 3 : 1)));
  89.   printf("psnr: %g, rmse: %g\n", psnr, rmse);
  90.  
  91.   fclose(inFile1);
  92.   fclose(inFile2);
  93.   fclose(outFile);
  94. }
  95.